#!/usr/bin/env python3
"""
验证脚本：查询刚创建的节点
"""

import asyncio
import json
import sys

try:
    from mcp import ClientSession, StdioServerParameters  # type: ignore
    from mcp.client.stdio import stdio_client  # type: ignore
except ImportError:
    print("Error: mcp not found. Please install the MCP SDK.")
    sys.exit(1)

async def verify_node():
    """验证节点的测试函数"""
    server_params = StdioServerParameters(
        command="uv",
        args=["run", "python", "main.py"],
        env=None
    )
    
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            # 初始化会话
            await session.initialize()
            
            print("正在查询 yanxx 标签的节点...")
            
            # 查询 yanxx 标签的节点
            result = await session.call_tool(
                "query_nodes",
                {
                    "label": "yanxx",
                    "limit": 10
                }
            )
            
            print("\n=== 查询结果 ===")
            for content in result.content:
                if hasattr(content, 'text'):
                    # 解析 JSON 结果
                    try:
                        data = json.loads(content.text)
                        if isinstance(data, list):
                            print(f"找到 {len(data)} 个节点:")
                            for i, node in enumerate(data, 1):
                                if isinstance(node, dict):
                                    print(f"  节点 {i}: ID={node.get('id', 'N/A')}, 属性={node.get('properties', {})}")
                                else:
                                    print(f"  节点 {i}: {node}")
                        else:
                            print(f"查询结果: {data}")
                    except json.JSONDecodeError:
                        print(content.text)
                else:
                    print(content)
            
            return result

if __name__ == "__main__":
    asyncio.run(verify_node())